package com.pe.pgn.clubpgn.webapp.controller.tipotarjetapersona;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpdTipoTarjetaPersona;
import com.pe.pgn.clubpgn.service.TipoTarjetaPersonaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/tipotarjetapersona/**")
public class TipoTarjetaPersonaController extends BaseFormController {
	@Autowired
	private TipoTarjetaPersonaService tipoTarjetaPersonaService;
    	
	@RequestMapping(value = "/tipotarjetapersonalist.html")
    public void listarTiposTarjetaPersona(Model model) {

		List<ClpdTipoTarjetaPersona> tiposTarjetaPersona = tipoTarjetaPersonaService.obtenerTiposTarjetasPersonas();
		model.addAttribute("tiposTarjetaPersona", tiposTarjetaPersona);
    }
	
	@RequestMapping(value = "/tipotarjetapersonaform.html", method = RequestMethod.GET)
	public @ModelAttribute("tipoTarjetaPersona") ClpdTipoTarjetaPersona verTipoTarjetaPersona(
				@RequestParam(value = "id", required = false) Long id, Model model) {

		if (id != null) {
			ClpdTipoTarjetaPersona tipoTarjetaPersona = tipoTarjetaPersonaService.obtenerTipoTarjetaPersona(id);
			return tipoTarjetaPersona;
		}
		
		ClpdTipoTarjetaPersona tipoTarjetaPersona = new  ClpdTipoTarjetaPersona();
		tipoTarjetaPersona.setStTipoTarjetaPersona(Boolean.TRUE);
		return tipoTarjetaPersona;
	}
	
	@RequestMapping(value = "/tipotarjetapersonaform.html", method = RequestMethod.POST)
	public String guardarTipoTarjetaPersona(@ModelAttribute("tipoTarjetaPersona") ClpdTipoTarjetaPersona tipoTarjetaPersona,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {

			if (result.hasErrors()) {
				model.addAttribute("tipoTarjetaPersona", tipoTarjetaPersona);
				return "/tipotarjetapersona/tipotarjetapersonaform";
			}

			tipoTarjetaPersonaService.guardarTipoTarjetaPersona(tipoTarjetaPersona);
			saveMessage(request, getText("tipotarjetapersonaform.added", tipoTarjetaPersona.getDeTipoTarjetaPersona(), 
					request.getLocale()));
			return "redirect:tipotarjetapersonalist.html";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/tipotarjetapersona/tipotarjetapersonaform";
		}

	}	
	
	@RequestMapping(value = "/eliminartipotarjetapersona.html")
	public String eliminarTipoTarjetaPersona(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request, Model model) {

		try {

			tipoTarjetaPersonaService.eliminarTipoTarjetaPersona(id);
			saveMessage(request, getText("tipotarjetapersonaform.deleted", request.getLocale()));
			return "redirect:tipotarjetapersonalist.html";
			
		} catch (Exception e) {
			ClpdTipoTarjetaPersona tipoTarjetaPersona = tipoTarjetaPersonaService.obtenerTipoTarjetaPersona(id);
			model.addAttribute("tipoTarjetaPersona", tipoTarjetaPersona);
			saveError(request, getText("errors.general", request.getLocale()));
			return "/tipotarjetapersona/tipotarjetapersonaform";
		}

	}		


}
